import router from '@/router';
import { store } from '@/store';
import Storage from '@/utils/storage';
import Utils from '@/utils/utils';

import { useService } from '@/utils/services';

// 检查患者通知
async function checkPatientNotice() {
  const actions = useService();
  /**
   * 如果开启了 则跳转到患者通知页面
   *
   * 扫码进入才需进入此流程
   */
  try {
    let result = await actions.hos.getNotification();
    if (result.notificationOpenStatus) {
      processChecker.content = result.content;
      // 跳转
      router.replace('/notice');
    } else {
      processChecker.next();
    }
  } catch {
    processChecker.next();
  }
}

// 检查患者信息
async function checkPatientInfo() {
  const actions = useService();

  /**
   * 如果开启了则跳转到患者表单
   */
  try {
    let result = await actions.hos.getEducation();
    if (result.formOpenStatus) {
      // 跳转
      router.replace('/patient-info');
    } else {
      processChecker.next();
    }
  } catch {
    processChecker.next();
  }
}

let stepTotal = 2;
let step = 0;
const processChecker = {
  inited: false,
  content: '',
  init() {
    processChecker.inited = true;

    step = 0;
    processChecker.next();
  },

  next() {
    if (!processChecker.inited) return;

    if (step >= stepTotal) {
      router.replace('/education');

      return processChecker.reset();
    }

    step++;
    stepChecker[step]();
  },

  reset() {
    processChecker.inited = false;
    step = 0;
  }
};

const stepChecker = {
  1: checkPatientNotice,
  2: checkPatientInfo
};

export default processChecker;
